﻿using Microsoft.EntityFrameworkCore;
using Shang.Microservice.Domain.Abstructions.SeedWork;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace PesticideManage.Domain.AggregatesModel.PesticideAggregate;
/// <summary>
/// 农药
/// </summary>
public class Pesticide : Entity
{
    internal const string RegexRegularExpression = @"^(PD|LS|WP|WL|F[A-Z])\d{8}$";
    public override int Id { get; set; }
    [JsonIgnore]
    public int UserId { get; set; }
    /// <summary>
    /// 产品名称
    /// </summary>
    [Display(Name = "产品名称")]
    [StringLength(20)]
    public string Name { get; set; } = null!;
    /// <summary>
    /// 类别
    /// </summary>
    [Display(Name = "类别")]
    public Category Category { get; set; }
    /// <summary>
    /// 第1位农药登记类别代码<br/>
    /// 1. PD 2. WP 3. 临时登记
    /// </summary>
    /// <remarks>
    /// <see cref="PesticideAggregate.PesticideRegistrationType"/>
    /// </remarks>
    public PesticideRegistrationType PesticideRegistrationType { get; set; }
    /// <summary>
    /// 登记证号 第2-7位农药登记类别代码
    /// </summary>
    [Display(Name = "登记证号")]
    [StringLength(10)]
    [MinLength(8)]
    //[RegularExpression(RegexRegularExpression, ErrorMessage = "登记证号格式错误，首写两字母请大写，数字为8位")]
    public required string RegistrationNumber { get; set; }
    /// <summary>
    /// 第8位,生产类型<br/>
    /// 1. 农药登记证持有人生产<br/>
    /// 2. 委托加工<br/>
    /// 3. 委托分装
    /// </summary>
    public ProductionType ProductionType { get; set; }
    /// <summary>
    /// 生产许可证号
    /// </summary>
    [Display(Name = "生产许可证号")]
    [StringLength(20)]
    public string? ProductionLicenseNumber { get; set; }

    /// <summary>
    /// 产品标准号
    /// </summary>
    [Display(Name = "产品标准号")]
    [StringLength(20)]
    public string? ProductStandardNumber { get; set; }
    /// <summary>
    /// 剂型
    /// </summary>
    [Display(Name = "剂型")]
    public DosageForm DosageForm { get; set; }

    /// <summary>
    /// 委托方企业名称
    /// </summary>
    [Display(Name = "农药登记持有企业名称")]
    [StringLength(20)]
    public string? EntrusterEnterprise { get; set; }
    /// <summary>
    /// 受托方企业名称
    /// </summary>
    [Display(Name = "生产企业名称")]
    [StringLength(20)]
    public string? EntrustedEnterprise { get; set; }
    /// <summary>
    /// 质量保证期
    /// </summary>
    [Display(Name = "质量保证期")]
    [StringLength(20)]
    public string? WarrantyPeriod { get; set; }
    /// <summary>
    /// 净含量
    /// </summary>
    [Display(Name = "净含量")]
    [StringLength(20)]
    public string? NetContent { get; set; }
    /// <summary>
    /// 生产地址
    /// </summary>
    [Display(Name = "生产地址")]
    [StringLength(50)]
    public string? Address { get; set; }
    /// <summary>
    /// 登记证持有人地址
    /// </summary>
    [Display(Name = "登记证持有人地址")]
    [StringLength(50)]
    public string? RegisteredAddress { get; set; }
    /// <summary>
    /// 客服电话
    /// </summary>
    [Display(Name = "客服电话")]
    [StringLength(50)]
    public string? Tel { get; set; }

    /// <summary>
    /// 有效日期
    /// </summary>
    [Display(Name = "有效日期")]
    [StringLength(20)]
    public string? EffectiveDate { get; set; }
    /// <summary>
    /// 生产批次
    /// </summary>
    [Display(Name = "生产批次")]
    [StringLength(20)]
    public string? ProductionBatch { get; set; }
    /// <summary>
    /// 总有效成分含量
    /// </summary>
    [Display(Name = "总有效成分含量")]
    [StringLength(20)]
    public string? TotalContent { get; set; }
    /// <summary>
    /// 有效成分1
    /// </summary>
    [Display(Name = "有效成分1")]
    [StringLength(20)]
    public string? ActiveIngredient1 { get; set; }
    /// <summary>
    /// 有效成分1含量
    /// </summary>
    [Display(Name = "有效成分1含量")]
    [StringLength(20)]
    public string? ActiveIngredientContent1 { get; set; }
    /// <summary>
    /// 有效成分2
    /// </summary>
    [Display(Name = "有效成分2")]
    [StringLength(20)]
    public string? ActiveIngredient2 { get; set; }
    /// <summary>
    /// 有效成分2
    /// </summary>
    [Display(Name = "有效成分2")]
    [StringLength(20)]
    public string? ActiveIngredientContent2 { get; set; }
    /// <summary>
    /// 有效成分3
    /// </summary>
    [Display(Name = "有效成分3")]
    [StringLength(20)]
    public string? ActiveIngredient3 { get; set; }
    /// <summary>
    /// 有效成分3
    /// </summary>
    [Display(Name = "有效成分3")]
    [StringLength(20)]
    public string? ActiveIngredientContent3 { get; set; }

    /// <summary>
    /// 使用说明
    /// </summary>
    [Display(Name = "使用说明")]
    public Instructions Instructions { get; set; } = null!;
    public string Get第2至7位()
    {

        string 第2_7位 = "";
        // 用于存储找到的数字
        int x = 6;
        var chars = new char[x];
        string input = RegistrationNumber;
        // 从字符串末尾开始向前搜索
        for (int i = input.Length - 1; i >= 0; i--)
        {
            if (char.IsDigit(input[i]))
            {
                chars[--x] = input[i];
                if (x == 0) // 已经找到了六个数字
                    break;
            }
        }
        if (x == 0)
        {
            第2_7位 = new string(chars);
        }
        else
        {
            throw new AggregateException("字符串中不足六位不连续的数字。");
        }

        return 第2_7位;
    }
    /// <summary>
    /// 组合前8位溯源码<br/>
    /// 第1位为该产品农药登记类别代码 <see cref="PesticideRegistrationType"/><br/>
    /// 第2-7位为该产品农药登记证号 (<see cref="RegistrationNumber"/>) 的后六位数字 <br/>
    /// 第8位为生产类型 <see cref="ProductionType"/>
    /// 
    /// </summary>
    /// <returns>前8位溯源码</returns>
    public string 组合前8位溯源码()
    {
        var b1 = (int)PesticideRegistrationType;
        var b2_7 = Get第2至7位();
        var b8 = (int)ProductionType;
        return $"{b1}{b2_7}{b8}";
    }
}
[Owned]
public class Instructions
{
    /// <summary>
    /// 产品特点
    /// </summary>
    [Display(Name = "产品特点")]
    public string Features { get; set; } = string.Empty;
    /// <summary>
    /// 使用范围和使用方法
    /// </summary>
    [Display(Name = "使用范围和使用方法")]
    public string? ScopeAndMethodOfUse { get; set; }
    /// <summary>
    /// 使用技术要求
    /// </summary>
    [Display(Name = "使用技术要求")]
    public string? TechnicalRequirementsForUse { get; set; }

    /// <summary>
    /// 注意事项
    /// </summary>
    [Display(Name = "注意事项")]
    public string? MattersNeedAttention { get; set; }
    /// <summary>
    /// 中毒急救措施
    /// </summary>
    [Display(Name = "中毒急救措施")]
    public string? FirstAidMeasuresForPoisoning { get; set; }
    /// <summary>
    /// 储存和运输方法
    /// </summary>
    [Display(Name = "储存和运输方法")]
    public string? StorageAndTransportationMethods { get; set; }

    /// <summary>
    /// 毒性及其标识
    /// </summary>
    [Display(Name = "毒性及其标识")]
    public Toxicity? ToxicityIdentification { get; set; }
    /// <summary>
    /// 备注
    /// </summary>
    [Display(Name = "备注")]
    [StringLength(20)]
    public string? Notes { get; set; }
}
public enum Category
{
    杀菌剂,
    杀虫剂,
    除草剂,
    杀螨剂,
    杀鼠剂,
    植物调节剂,
    卫生杀虫剂,
}
public enum ProductionType
{
    农药登记证持有人生产 = 1, 委托加工, 委托分装
}
public enum Toxicity
{
    微毒,
    低毒,
    中毒,
    高毒,
    剧毒
}
